<template>
  <div class="cinema-detail">
    <!-- 头部标题及返回 -->
    <van-nav-bar @click-left="onClickLeft">
      <template #left>
        <van-icon name="arrow-left" class="arrow" />
      </template>
      <template #title>
        <span>{{ CinemaDetail.cinemaName }}</span>
      </template>
    </van-nav-bar>
    <!-- 地点 -->
    <div class="place-box">
      <div class="left-box">
        <div class="cinema-name">{{ CinemaDetail.cinemaName }}</div>
        <div class="cinema-location">{{ address }}</div>
      </div>
      <div class="icon-location">
        <van-icon name="location-o" size="30" color="skyblue" />
      </div>
    </div>
    <!-- Swiper -->
    <div class="swiper-box">
      <div class="swiper-container">
        <div class="swiper-wrapper">
          <div
            v-for="movie in movies"
            :key="movie.id"
            class="swiper-slide"
            :style="`background-image: url(${movie.img})`"
          ></div>
        </div>
      </div>
      <div class="swiper-bg" ref="boxRef"></div>
      <div class="triangle"></div>
    </div>
    <!-- 电影介绍 -->
    <div class="FilmIntroduction">
      <div class="movie-title">
        <span class="movie-name">{{ ActiveMovie.nm }}</span>
        <span class="movie-score" v-if="+ActiveMovie.sc">
          {{ ActiveMovie.sc }}
          <i>分</i>
        </span>
        <span class="movie-score" v-else>
          {{ ActiveMovie.wish }}
          <i>人想看</i>
        </span>
      </div>
      <div class="movie-desc">{{ ActiveMovie.desc }}</div>
    </div>
    <!-- 场次列表 -->
    <van-tabs v-model="active" animated class="cc-box">
      <van-tab v-for="obj in ActiveMovie.shows" :key="obj.dateShow">
        <template #title> {{ obj.dateShow }} </template>
        <div class="cc-list" v-for="cc in obj.plist" :key="cc.seqNo">
          <!-- 时间 -->
          <div class="cc-time">
            <div class="cc-begin">{{ cc.tm }}</div>
            <div class="cc-end">
              {{ EndTime(cc.dt, cc.tm, ActiveMovie.dur) }}散场
            </div>
          </div>
          <!-- 语言与地点 -->
          <div class="cc-info">
            <div class="lan">{{ cc.lang }}&nbsp;{{ cc.tp }}</div>
            <div class="hall">{{ cc.th }}</div>
          </div>
          <!-- 价格 -->
          <div class="cc-price">
            <span class="fh">￥</span>
            <span class="price">{{ cc.vipPrice }}</span>
          </div>
          <!-- 购票 -->
          <div
            class="cc-buy"
            @click="buyTicket(cc.seqNo, userId, ActiveMovie.id)"
          >
            购票
          </div>
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import Swiper from "swiper"
import "swiper/dist/css/swiper.min.css"
import { getCinemaDetail } from "@/api/cinemaDetail"
import { addOrder } from "@/api/cart"

export default {
  props: ["cinemaId", "movieId"],
  data() {
    return {
      CinemaDetail: {},
      address: "",
      movies: [],
      activeIndex: 0,
      ActiveMovie: {},
      active: 0
    }
  },
  created() {
    if (!this.$store.state.user.isLogin) {
      this.$router.push({ path: '/login' })
    }
    const user = this.$store.state.user.user
    this.userId = user.id || 0
    console.log(this.userId)
    this.getCinemaDetail()
  },
  methods: {
    async getCinemaDetail() {
      const res = await getCinemaDetail(this.cinemaId)
      this.CinemaDetail = res.data.data.showData
      this.address = res.data.data.cinemaData.addr
      this.movies = res.data.data.showData.movies
      this.movies.forEach((item) => {
        item.img = item.img.replace(/\/w.h\//, "/90.130/")
      })
      console.log(res.data.data.showData.movies)
      // 跳到指定的电影
      if (this.movieId) {
        this.activeIndex = this.movies.findIndex(
          (movie) => movie.id === +this.movieId
        )
        this.swiper.slideTo(this.activeIndex)
      } else {
        this.activeIndex = 0
      }
      this.$refs.boxRef.style.backgroundImage =
        "url(" + this.movies[this.activeIndex].img + ")"
      this.ActiveMovie = this.movies[this.activeIndex]
    },
    onClickLeft() {
      this.$router.go(-1)
    },
    EndTime(dt, start, dur) {
      const date = new Date(`${dt}T${start}`)
      date.setMinutes(date.getMinutes() + +dur)
      return `${String(date.getHours()).padStart(2, 0)}:${String(
        date.getMinutes()
      ).padStart(2, 0)}`
    },
    async buyTicket(seqNo, movieId, userId) {
      const res = await addOrder(seqNo, movieId, userId)
      console.log(res.data.data.order.orderId)
      this.$router.push({
        path: `/cart/succeed/${res.data.data.order.orderId}`
      })
    }
  },
  mounted() {
    const self = this
    this.swiper = new Swiper(".swiper-container", {
      // 轮播图swiper相关属性
      autoplay: false,
      pagination: ".swiper-pagination",
      effect: "coverflow",
      grabCursor: true,
      centeredSlides: true,
      slidesPerView: "auto",
      coverflow: {
        rotate: 50,
        stretch: 0,
        depth: 100,
        modifier: 1,
        slideShadows: true
      },
      observer: true,
      observeParents: true,
      slideToClickedSlide: true,
      onTransitionStart: function (swiper) {
        self.activeIndex = swiper.activeIndex
        self.ActiveMovie = self.movies[swiper.activeIndex]
        self.$refs.boxRef.style.backgroundImage =
          "url(" + self.movies[swiper.activeIndex].img + ")"
      }
    })
  }
}
</script>

<style lang="scss" scoped>
.cinema-detail {
  // padding-bottom: 50px;
  // 头部
  .van-nav-bar {
    height: 46px;
    background-color: #e54847;
    .arrow {
      color: #fff;
      // font-size: 24px;
    }
    /deep/ .van-nav-bar__title {
      max-width: 70%;
      color: #fff;
      font-size: 18px;
    }
  }
  // 地点
  .place-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 0 15px 15px;
    box-sizing: border-box;
    .left-box {
      width: 76%;
      .cinema-name {
        font-size: 17px;
        line-height: 24px;
        font-weight: 700;
        color: #333;
      }
      .cinema-location {
        margin-top: 2px;
        font-size: 13px;
        line-height: 18.5px;
        color: #999;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
        margin-top: 5px;
      }
    }
    .icon-location {
      height: 32px;
      width: 14%;
      border-left: 1px solid;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  // Swiper
  .swiper-box {
    position: relative;
    overflow: hidden;
    .swiper-container {
      position: relative;
      z-index: 999;
      width: 100%;
      padding: 15px 0;
      .swiper-slide {
        background-position: center;
        background-size: cover;
        width: 90px;
        height: 130px;
      }
    }
    .swiper-bg {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      filter: blur(30px);
      -webkit-filter: blur(30px);
    }
    .triangle {
      position: absolute;
      border: 10px solid transparent;
      border-bottom: 10px solid #fff;
      bottom: 0;
      left: 50%;
      margin-left: -5px;
    }
  }
  // 电影介绍
  .FilmIntroduction {
    padding: 10px 0;
    text-align: center;
    border-bottom: 1px solid #ccc;
    .movie-title {
      .movie-name {
        font-size: 20px;
        font-weight: bold;
        margin-right: 5px;
      }
      .movie-score {
        color: #ffb400;
        font-size: 16px;
        i {
          font-size: 12px;
          font-style: normal;
        }
      }
    }
    .movie-desc {
      font-size: 12px;
      color: #999;
      margin: 10px auto;
      width: 80%;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }
  }
  // 场次cc列表
  .cc-box {
    .cc-list {
      display: flex;
      align-items: center;
      padding: 17px 12px;
      .cc-time {
        margin-right: 20px;
        .cc-begin {
          font-size: 20px;
          color: #333;
          line-height: 1;
        }
        .cc-end {
          font-size: 13px;
          color: #797d82;
          margin-top: 2px;
        }
      }
      .cc-info {
        flex: 1;
        .lan {
          line-height: 18px;
          font-size: 13px;
          color: #333;
        }
        .hall {
          margin-top: 7px;
          font-size: 11px;
          color: #999;
        }
      }
      .cc-price {
        color: #ff5f16;
        margin-right: 20px;
        .fh {
          font-size: 12px;
        }
        .price {
          font-size: 15px;
        }
      }
      .cc-buy {
        padding: 5px 10px;
        font-size: 12px;
        border: 1px solid #f06762;
        border-radius: 50px;
        color: #f03d37;
      }
    }
  }
}
</style>
